﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace Nivdal.Sponsors
{
    public partial class ChangePassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

           protected void btnBack_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex--;
            this.ErrorLabel.Text = "";
        }

           protected void btnNext_Click(object sender, EventArgs e)
           {
               string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
               System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(conn);
               System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
               dbCommand.CommandText = "Nivdal_NewSponsorChangePassword";
               dbCommand.CommandType = CommandType.StoredProcedure;
               dbCommand.Connection = dbConnection;

               SqlParameter dbParam_SponsorId = new SqlParameter();
               dbParam_SponsorId.ParameterName = "@SponsorId";
               dbParam_SponsorId.Value = Session["SponsorId"].ToString();
               dbParam_SponsorId.DbType = DbType.String;
               dbParam_SponsorId.Size = 64;
               dbCommand.Parameters.Add(dbParam_SponsorId);

               SqlParameter dbParam_OldPassword = new SqlParameter();
               dbParam_OldPassword.ParameterName = "@OldPassword";
               dbParam_OldPassword.Value = this.OldPasswordTextBox.Text.Trim();
               dbParam_OldPassword.DbType = DbType.String;
               dbCommand.Parameters.Add(dbParam_OldPassword);

               SqlParameter dbParam_NewPassword = new SqlParameter();
               dbParam_NewPassword.ParameterName = "@NewPassword";
               dbParam_NewPassword.Value = this.NewPasswordTextBox.Text.Trim();
               dbParam_NewPassword.DbType = DbType.String;
               dbCommand.Parameters.Add(dbParam_NewPassword);
               SqlParameter dbParam_Answer = new SqlParameter();
               dbParam_Answer.ParameterName = "@Answer";
               dbParam_Answer.Value = "";
               dbParam_Answer.DbType = DbType.String;
               dbParam_Answer.Direction = ParameterDirection.Output;
               dbParam_Answer.Size = 64;
               dbCommand.Parameters.Add(dbParam_Answer);

               try
               {

                   dbConnection.Open();
                   dbCommand.ExecuteScalar();
                   string Answer = dbParam_Answer.Value.ToString();
                   if (Answer == "")
                       this.ErrorLabel.Text = "Error";
                   else
                   {

                       MultiView1.ActiveViewIndex++;
                       this.ResetButton.Visible = false;
                       GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
                       GND.SendPendingMails();
                       //this.ErrorLabel.Text = Answer;
                   }
               }
               catch (Exception SQLDBException)
               {
                   this.ErrorLabel.Text = Convert.ToString(SQLDBException);
                   return;

               }

               finally
               {
                   dbConnection.Close();
               }

           }

        protected override void OnPreRender(EventArgs e)
        {
            
            //btnBack.Visible = MultiView1.ActiveViewIndex > 0;
            btnNext.Visible = MultiView1.ActiveViewIndex < MultiView1.Views.Count - 1;
            //btnSend.Visible = MultiView1.ActiveViewIndex == MultiView1.Views.Count - 1;
            base.OnPreRender(e);
        }

        protected void ResetButton_Click(object sender, EventArgs e)
        {

            MultiView1.ActiveViewIndex = 0;
            this.ErrorLabel.Text = "";
            this.OldPasswordTextBox.Text = "";
            this.NewPasswordTextBox.Text = "";
            this.ConfirmNewPasswordTextBox.Text = "";


        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
           

        }
    }
}